#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n = arr.size();
        vector<vector<int>> dp(n, vector(n, 2));
        unordered_map<int, int> hash;
        for (int i = 0;i < n;i++)
        {
            hash[arr[i]] = i;
        }
        int ret = 2;
        for (int j = 2; j < n;j++)
        {
            for (int i = 1;i < j;i++)
            {
                if (hash.count(arr[j] - arr[i]) && hash[arr[j] - arr[i]] < i)
                {
                    dp[i][j] = dp[hash[arr[j] - arr[i]]][i] + 1;
                }
                ret = max(dp[i][j], ret);
            }
        }
        if (ret == 2) return 0;
        else return ret;
    }
};